package br.ufmg.vod.wbr.algorithims.components;

import javolution.util.FastList;
import br.ufmg.vod.collections.IntQueueStack;
import br.ufmg.vod.collections.IntStack;
import br.ufmg.vod.wbr.algorithims.bfs.BFSData;
import br.ufmg.vod.wbr.algorithims.bfs.FastBFS;
import br.ufmg.vod.wbr.api.Graph;

public class WeaklyConnectedComponentBFS implements Runnable {
	

	private final Graph graph;
	
	private FastList<int[]> components;
	private int[] giantComponent;
	private FastBFS bfs;

	private BFSData bfsData;

	public WeaklyConnectedComponentBFS(Graph graph){
		this.graph = graph;
		components = new FastList<int[]>();
		bfs = new FastBFS(graph);
		bfsData = bfs.getData();
	}
	
	@Override
	public void run() {
		wccAllVertex();
	}

	private void wccAllVertex() {
		for (int u = 0; u < this.graph.getNumVertex(); u++) {
			if(bfsData.colors[u] == BFSData.WHITE){
				this.bfs.bfsSingleVertex(u);
				int[] component = bfsData.visitedQueue.toArray( new int[bfsData.visitedQueue.size()] );
				giantComponent = giantComponent == null || giantComponent.length < component.length ? component : giantComponent;
				this.components.add(component);
				bfsData.visitedQueue.clear();
			}
		}
	}



	public Graph getGraph() {
		return graph;
	}


	public FastList<int[]> getComponents() {
		return components;
	}


	public int[] getGiantComponent() {
		return giantComponent;
	}


	
	


}
